/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.server.sql;

import com.sinai.mshab.client.MSHABConfig;

/**
 * This class offers a place to store information about database fields.
 * Information such as the field of the table where they are stored, the field
 * of the field itself and the value that field holds.
 * 
 * @author Arthur Kalmenson
 */
public class DbField {

	/**
	 * The field of the table where the field is stored.
	 */
	private String table;

	/**
	 * The field of the database field.
	 */
	private String field;

	/**
	 * The value this field holds.
	 */
	private String value;

	/**
	 * Creates a new DbField. Note, all parameters must be instantiated and not
	 * empty otherwise a {@link IllegalArgumentException} will be thrown.
	 * 
	 * @param table
	 *            the database table where this field is stored.
	 * @param field
	 *            the field of the field.
	 */
	public DbField(String table, String field) {

		// check to insure that the input is not null.
		if (table == null || field == null) {
			throw new IllegalArgumentException(
					MSHABConfig.MSHAB_ERROR_NULL_ARGS);
		}

		// check to insure that the input is not empty.
		if (table.trim().isEmpty() || field.trim().isEmpty()) {
			throw new IllegalArgumentException(
					MSHABConfig.MSHAB_ERROR_EMPTY_ARGS);
		}

		this.table = table;
		this.field = field;
	}

	/**
	 * Creates a new DbField. Note, all parameters must be instantiated and not
	 * empty otherwise a {@link IllegalArgumentException} will be thrown.
	 * 
	 * @param table
	 *            the database table where this field is stored.
	 * @param field
	 *            the field of the field.
	 * @param value
	 *            the value the field holds.
	 */
	public DbField(String table, String field, String value) {

		// check to insure that the input is not null.
		if (table == null || field == null || value == null) {
			throw new IllegalArgumentException(
					MSHABConfig.MSHAB_ERROR_NULL_ARGS);
		}

		// check to insure that the input is not empty.
		if (table.trim().isEmpty() || field.trim().isEmpty()) {
			throw new IllegalArgumentException(
					MSHABConfig.MSHAB_ERROR_EMPTY_ARGS);
		}

		this.table = table;
		this.field = field;
		this.value = value;
	}

	/**
	 * Returns the field of the database table where this field is stored.
	 * 
	 * @return the field of the database table where this field is stored.
	 */
	public String getTable() {
		return table;
	}

	/**
	 * Returns the field of this field.
	 * 
	 * @return the field of this field.
	 */
	public String getField() {
		return field;
	}

	/**
	 * Returns the value that this field holds. Null will be returned if there
	 * is no value.
	 * 
	 * @return the value that this field holds.
	 */
	public String getValue() {
		return value;
	}

	public boolean equals(Object obj) {

		boolean result = true;

		if (obj == null) {
			result = false;
		} else if (!(obj instanceof DbField)) {
			result = false;
		} else {

			// now we know it's a DbField, we need to compare DbField's various
			// fields.
			DbField compareField = (DbField) obj;

			// both table and field must always be instantiated and not empty,
			// so the only difference between them is that one can have a value
			// and the other one can be missing a value.
			if (!compareField.getTable().equals(table)) {
				result = false;
			} else if (!compareField.getField().equals(field)) {
				result = false;
			} else if (compareField.getValue() == null && value != null
					|| compareField.getValue() != null && value == null) {
				result = false;
			} else if (compareField.getValue() != null && value != null) {
				if (!compareField.getValue().equals(value)) {
					result = false;
				}
			}
		}

		return result;
	}
	
	public int hashCode() {
		int hash = 0;
		
		hash += table.hashCode() + field.hashCode();
		
		if (value != null) {
			hash += value.hashCode();
		}
		
		return hash;
	}
}
